/*实现级联勾选开始*/
var nodeCheckedSilent = false;
function nodeChecked (event, node){
    if(nodeCheckedSilent){
        return;
    }
    nodeCheckedSilent = true;
    checkAllParent(node);
    checkAllSon(node);
    nodeCheckedSilent = false;
}

var nodeUncheckedSilent = false;
function nodeUnchecked  (event, node){
    if(nodeUncheckedSilent)
        return;
    nodeUncheckedSilent = true;
    uncheckAllParent(node);
    uncheckAllSon(node);
    nodeUncheckedSilent = false;
}

//选中全部父节点
function checkAllParent(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)){
        return;
    }else{
        checkAllParent(parentNode);
    }
}
//取消全部父节点
function uncheckAllParent(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    var siblings = $('#tree').treeview('getSiblings', node.nodeId);
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)) {
        return;
    }
    var isAllUnchecked = true;  //是否全部没选中
    for(var i in siblings){
        if(siblings[i].state.checked){
            isAllUnchecked=false;
            break;
        }
    }
    if(isAllUnchecked){
        uncheckAllParent(parentNode);
    }

}

//级联选中所有子节点
function checkAllSon(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            checkAllSon(node.nodes[i]);
        }
    }
}
//级联取消所有子节点
function uncheckAllSon(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            uncheckAllSon(node.nodes[i]);
        }
    }
}
/*实现级联勾选结束*/
function fmtState(value,row){
    return value==1?"启用":"禁用";
}
$(document).ready(function(){
    $("#grant").dialog({
        title:'角色授权',
        btnName:'确定',
        btnBind:function(){
            var rows=$("#roleTb").bootstrapTable("getSelections");
            var params="roleId="+rows[0].id;
            var arr=$("#tree").treeview("getChecked");
            $(arr).each(function(){
                params=params+"&mids="+this.id;
            });
            $.post(ctx+"sys/permission/grant",params,function(msg){
                toastr.warning(msg);
                $("#grant").dialog("close");
                $("#roleTb" ).bootstrapTable("uncheckAll");
            },'text');


        }
    });
});
function prepareGrant(){
    var rows=$("#roleTb").bootstrapTable("getSelections");
    if(rows.length==0){
        swal({
            title:"信息提示",
            text:"请选择一项待授权！",
            type:"warning",
            confirmButtonText:"确定"
        });
        return;
    }
    var url=ctx+"sys/permission/tree";
    var params={roleId:rows[0].id};
   $.getJSON(url,params,function(arr){
       $("#tree").treeview({
           data:arr,
           multiSelect:false,
           showCheckbox:true,
           onNodeChecked:nodeChecked ,
           onNodeUnchecked:nodeUnchecked
       });

        $("#grant").dialog("open");
   });
}